package com.juzhi.dvrapp.activity;

import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.bigkoo.pickerview.TimePickerView;
import com.juzhi.dvrapp.R;
import com.juzhi.dvrapp.bean.Device;
import com.juzhi.dvrapp.bean.MessageModel;
import com.juzhi.dvrapp.manager.DeviceManager;
import com.juzhi.dvrapp.utils.Config;
import com.juzhi.dvrapp.widget.DividerItemDecoration;
import com.mcxtzhang.commonadapter.rv.CommonAdapter;
import com.mcxtzhang.commonadapter.rv.ViewHolder;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import butterknife.BindView;


public class DeviceMessageActivity extends BaseAcitivity {


    @BindView(R.id.activity_message_rv)
    RecyclerView mRecyclerView;
    private Device device;
    private CommonAdapter<MessageModel> adapter;
    private SimpleDateFormat format;
    private TimePickerView pvTime;
    private List<MessageModel> message;

    @Override
    public int getLayoutId() {
        return R.layout.activity_device_message;
    }

    @Override
    protected void initView() {

        initHeaderView();
        setFunctionImg(ContextCompat.getDrawable(this,R.drawable.ic_device_calendar));

        format = new SimpleDateFormat("yyyy-MM-dd");
        String dataTime = this.format.format(new Date());
        setTitleText(dataTime);

        initTimePicker(); //  初始化时间选择器


        String ssid = getIntent().getStringExtra(Config.SSID);
        DeviceManager deviceManager = DeviceManager.getDeviceManager(this);
        device = deviceManager.getDevice(ssid);


        message = device.getMessage();
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mRecyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL_LIST));
        adapter = new CommonAdapter<MessageModel>(this, message,R.layout.activity_messgae_item) {

            @Override
            public void convert(ViewHolder viewHolder, MessageModel messageModel) {
                viewHolder.setText(R.id.message_item_name,messageModel.getDeviceName());
//                viewHolder.setText(R.id.message_item_tpye,messageModel.getDeviceName());
                viewHolder.setText(R.id.message_item_time, stampToDate(messageModel.getTime()));

            }
        };

        mRecyclerView.setAdapter(adapter);

    }

    @Override
    protected void function() {
        super.function();
        pvTime.show();
    }

    private String getTime(Date date) {//可根据需要自行截取数据显示
        return format.format(date);
    }

    private void initTimePicker() {
        //控制时间范围(如果不设置范围，则使用默认时间1900-2100年，此段代码可注释)
        //因为系统Calendar的月份是从0-11的,所以如果是调用Calendar的set方法来设置时间,月份的范围也要是从0-11
        Calendar selectedDate = Calendar.getInstance();

        Calendar startDate = Calendar.getInstance();
        startDate.set(2013, 0, 23);

        Calendar endDate = Calendar.getInstance();
        endDate.set(2019, 11, 28);
        //时间选择器
        pvTime = new TimePickerView.Builder(this, new TimePickerView.OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, View v) {//选中事件回调
                // 这里回调过来的v,就是show()方法里面所添加的 View 参数，如果show的时候没有添加参数，v则为null

                /*btn_Time.setText(getTime(date));*/
//                TextView btn = (TextView) v;
//                btn.setText(getTime(date));
                setTitleText(getTime(date));
                adapter.setDatas(device.getMessage());
            }
        })
                .setType(new boolean[]{true,true,true,false,false,false})
                .setLabel("", "", "", "", "", "") //设置空字符串以隐藏单位提示   hide label
//                .setDividerColor(R.color.cutting_line)
                .setContentSize(20)
                .setTitleText(getStringText(R.string.select_time))
                .isCyclic(true)
                .isDialog(true)  //设置显示为Dialog
                .setDate(selectedDate)
                .setLineSpacingMultiplier(2.0f)
                .setRangDate(startDate, endDate)
                .setOutSideCancelable(false)
                .build();

    }

    /*
  * 将时间戳转换为时间
  */
    public static String stampToDate(String s){
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long lt = new Long(s);
        Date date = new Date(lt);
        res = simpleDateFormat.format(date);
        return res;
    }

}
